﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Greve.Forms
{
    public partial class ConnectToServer : Form
    {
        Thread thr;
        NetworkClient client;
		Judge_ui judge;
        Resultat result;
        String type;
        String loginName;
        public ConnectToServer(String t, String loginName)
        {
            this.loginName = loginName;
            type = t;
            InitializeComponent();
            client = new NetworkClient();
            
        }
        private void setCurrentJumper()
        {
            judge.setNewJumper(client.lastSentMessage.Split(' ')[0]);
        }

        private void updateResult()
        {
            String str = client.lastSentMessage;
            result.addPerson(str);
        }

        private void judgeOK(Judge_ui j, EventArgs e)
        {
            client.send("Values " + judge.getValues());
        }
        private void requestResult(Resultat r, EventArgs e)
        {
            //client.send("RequestResult");
            Resultat.updateList();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if(!client.on)
               client.start(Input.Text, 3000);


            if (client.on)
            {
                if (type.Equals("judge"))
                {
                    this.Hide();
                    client.send(NetworkServer.registerID + loginName);
                    judge = new Judge_ui();
                    thr = new Thread(client.signalFromServer);
                    client.onUpdate += new NetworkClient.TickHandler(setCurrentJumper);
                    thr.Start();
                    judge.Tick += new Judge_ui.TickHandler(judgeOK);
                    judge.ShowDialog();
                    client.shutdown();
                    thr.Abort();
                    thr.Join();
                }
                else
                {
                    this.Hide();
                    client.send(NetworkServer.registerID + "[Result]");
                    result = new Resultat();
                    thr = new Thread(client.signalFromServer);
                    client.onUpdate += new NetworkClient.TickHandler(updateResult);
                    result.Tick += new Resultat.TickHandler(requestResult);
                    thr.Start();
                    result.ShowDialog();
                    client.shutdown();
                    thr.Abort();
                    thr.Join();
                }
            }
            /*
            if (setName.Text.Length > 0)
            {
                if (setName.Text.Contains("login: "))
                {
                    if (!(setName.Text.Length > "login: ".Length))
                        
                    else
                    {
                        string t = setName.Text;
                        t.Remove(0, "login: ".Length);

                        client.start(t, 3000);

                    }
                }
                else if (client.on)
                    client.send(setName.Text);

            }
            else if (client.on)
            {
                if (!checkBox1.Checked)
                {
                    client.send(points_text.Text);
                }
                else
                {
                    client.send(NetworkServer.penaltyID + textBox_penalty.Text);
                }
            }*/
        }
    }
}
